package com.example.king.ffmpegexample;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

    private NativeUtil nativeUtil;
    private final int PERMISSIONS_REQUEST_RESULT_CODE = 1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nativeUtil = new NativeUtil();


        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(nativeUtil.stringFromJNI());

//        nativeUtil.decodeMedia2Yuv("/mnt/sdcard/DCIM/Camera/VID_20180120_123024.mp4",
//                Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.yuv",
//                Environment.getExternalStorageDirectory().getAbsolutePath() + "/test_audio.pcm");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {//系统版本大于11
            if (!Environment.isExternalStorageManager()) {//没有授权获取全部权限
                //跳转到设置界面引导用户打开
                Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                intent.setData(Uri.parse("package:com.example.king.ffmpegexample"));
                startActivityForResult(intent, PERMISSIONS_REQUEST_RESULT_CODE, null);
            } else {// 有了权限后，再申请读写权限，
                getPermissions();
            }
        } else {//系统版本小于11
            getPermissions();
        }



    }

    private static final String TAG = "MainActivityTag";

    private void getPermissions() {

        Log.d(TAG, "getPermissions");
        PermissionsUtils.getInstance().checkPermissions(
                this, new String[]{
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                },
                new PermissionsUtils.IPermissionsResult() {
                    @Override
                    public void passPermissions() {


                        Log.d(TAG, "permission get success ！！！");
                        startActivity(new Intent(MainActivity.this, H264ToRgbAndroidActivity.class));

                    }

                    @Override
                    public void notPermissions() {
                        Log.d(TAG, "notPermissions！！！");
                    }
                }
        );
    }

}
